<template>
  <div class="login">
    <van-nav-bar title="会员登录"
     left-arrow
     @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将会进行自动注册</p>
      </div>
      <div class="form">
        <div class="form-item">
          <input v-model="mobile" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input v-model="picCode" class="inp" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img v-if="captchaCode" :src="captchaCode" alt="" @click="getCaptchaCode">
        </div>
        <div class="form-item">
          <input v-model="smsCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button :disabled="disabled" @click="getCode">
            {{ second===60?'获取验证码': second+'秒后重试'}}
          </button>
        </div>
      </div>

      <div @click="login" class="login-btn">登录</div>
    </div>
  </div>
</template>

<script>
import { getCaptchaCode, getCode, login } from '@/api/login'
export default {
  name: 'LoginPage',
  data () {
    return {
      captchaCode: '', // 图形验证码url
      captchaKey: '', // 图形验证码key
      mobile: '', // 手机号
      picCode: '', // 用户输入的图形验证码
      totalSecond: 60, // 获取验证码总秒数
      second: 60, // 当前秒数
      timer: null, // 定时器id
      smsCode: '', // 用户输入的验证码
      disabled: false// 控制发送短信按钮的点击状态

    }
  },
  created () {
    this.getCaptchaCode()
  },
  methods: {
    // 获取图形验证码
    async getCaptchaCode () {
      const { data: { base64, key } } = await getCaptchaCode()
      this.captchaCode = base64
      this.captchaKey = key
    },
    // 校验
    valid () {
      // 手机号
      if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      // 验证码
      if (!/^\w{4}$/.test(this.picCode)) {
        this.$toast('请输入正确的验证码')
        return false
      }
      return true
    },
    // 获取验证码
    async getCode () {
      // 校验
      if (!this.valid()) {
        return
      }
      // 调接口获取验证码
      await getCode(this.picCode, this.captchaKey, this.mobile)
      // 开启倒计时
      if (!this.timer && this.second === this.totalSecond) {
        // 节流处理
        this.disabled = true
        this.$toast('获取短信验证码成功')
        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            clearInterval(this.timer)
            this.timer = null
            this.second = this.totalSecond
            this.disabled = false
          }
        }, 1000)
      }
    },
    async login () {
      if (!this.valid()) {
        return
      }
      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的验证码')
        return
      }
      const res = await login(this.mobile, this.smsCode)
      this.$store.commit('user/setUserInfo', res.data)
      this.$toast('登录成功!!')
      // 判断是跳回哪里的
      const fullPath = this.$route.query.fullUrl
      if (fullPath) {
        this.$router.replace(fullPath)
        return
      }
      // 正常跳转
      this.$router.replace('/')
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
